<?php

namespace App\Models\Purchase;


use App\Models\Balance\Balance;
use App\Models\Custom\Custom;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

class PurchaseOrderProcess extends Model
{
    protected $table = 'purchase_order_process';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];

    /**
     *获取列表
     */
    public static function getList(Request $request, $cpid, $manger_id = 0, $isgetall = 0)
    {
        $query = self::with('suborder')->where(['cpid' => $cpid, 'is_del' => 1])->orderBy('create_at', 'desc');
        if ($manger_id > 0) {
            $query->where('manger_id', $manger_id);
        }

        $pa_type = $request->pa_type;
        if (in_array($pa_type, [1, 2, 3,4])) {
            $query->where('pa_type', $pa_type);
        }
        $pa_id = intval($request->pa_id);
        if ($pa_id>0) {
            $query->where('pa_id', $pa_id);
        }
        $pa_settle_company_id = intval($request->pa_settle_company_id);
        if ($pa_settle_company_id>0) {
            $query->where('pa_settle_company_id', $pa_settle_company_id);
        }

        $is_finish = $request->is_finish;
        if (in_array($is_finish, [1, 2])) {
            $query->where('is_finish', $is_finish);
        }

        $company_name = $request->company_name; //供应商名称
        if ($company_name != '') {
            $query->where('company_name', 'like', '%' . $company_name . '%');
        }

        $start_time = strtotime($request->start_time) > 0 ? strtotime($request->start_time) : intval($request->start_time);
        $end_time = strtotime($request->end_time) > 0 ? strtotime($request->end_time) : intval($request->end_time);

        if ($start_time > 0 && $end_time > 0) {
            $query->where('create_at', '>=', $start_time);
            $query->where('create_at', '<=', $end_time);
        }

        $get_balance = intval($request->get_balance); //获取供应商余额
        $custom_id=intval($request->custom_id);
        $customBalance = 0;
        if($get_balance>0 && $custom_id>0)
        {

            $customBalance = Balance::where('custom_id', $custom_id)->first();
            
            $customBalance = $customBalance?$customBalance->balance:0;


            $query->where('custom_id', $custom_id);

        }



        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();

            $newdata = [
                'list' => $list['data'],
                'page' => [
                    'totalCount' => $list['total'],
                    'currentPage' => $list['current_page'],
                    'totalPage' => $list['last_page']
                ]
            ];
            if($get_balance>0 && $custom_id>0)
            {
                $newdata['customBalance'] = $customBalance;
            }
            return $newdata;
        } else {
            $list = $query->get()->toArray();
            return ['list' => $list];
        }
    }


    /**
     * 获取子订单
     */
    public function suborder()
    {
        return $this->hasMany(PurchaseOrderProcessSub::class, 'process_order_id', 'id')->where('is_del', 1);
    }

    public function getOrderWeightAttribute($value)
    {
        return $value / 10;
    }

    public function getStockinWeightAttribute($value)
    {
        return $value / 10;
    }

    public function getWarehousingWeightAttribute($value)
    {
        return $value / 10;
    }

    public function getResidueWeightAttribute($value)
    {
        return $value / 10;
    }
}
